const fp = require('loadsh/fp')
const cars = [
  {name: 'a', horsepower: 660, dollar_value: 700000, in_stock: true},
  {name: 'b', horsepower: 760, dollar_value: 800000, in_stock: false},
  {name: 'c', horsepower: 860, dollar_value: 900000, in_stock: true},
  {name: 'd', horsepower: 960, dollar_value: 100000, in_stock: false},
  {name: 'e', horsepower: 160, dollar_value: 200000, in_stock: true},
  {name: 'f', horsepower: 260, dollar_value: 300000, in_stock: false},
]
// console.log(fp.prop('in_stock', cars[5]))
// let isLastInStock = fp.flowRight(fp.prop('in_stock'),fp.last)
// console.log(isLastInStock(cars))

// let isLFirstInname = fp.flowRight(fp.prop('name'),fp.first)
// console.log(isLFirstInname(cars))


// let _average = function (xs) {
//   return fp.reduce(fp.add, 0, xs)/ xs.length
// }
// let averageDollarValue = fp.flowRight(_average,fp.map(car => car.dollar_value))
// console.log(averageDollarValue(cars))

// let _underscore = fp.replace(/\W+/g,'_')
// let sanitizeNames = fp.flowRight(_underscore,fp.lowerCase)
// console.log(sanitizeNames('HELLO WORLD'))


class Container {
  static of (value) {
    return new Container(value)
  }
  constructor (value) {
    this._value = value
  }
  map (fn) {
    return Container.of(fn(this._value))
  }
}
class Maybe {
  static of (x) {
    return new Maybe(x)
  }
  constructor (x) {
    this._value = x
  }
  isNothing () {
    return this._value === null || this._value === undefined
  }
  map (fn) {
    console.log(Maybe.of(fn(this._value)))
    return this.isNothing() ? this : Maybe.of(fn(this._value))
  }
}

// let maybe = Maybe.of([5,6,1])
// let ex1 = maybe
//           .map(fp.map(fp.add(1)))
// console.log(ex1)

// let xs = Container.of(['do','ad','cc'])
// let ex2 = xs.map(fp.first)
// console.log(ex2)

// let safeProp = fp.curry(function (x,o) {
//   return Maybe.of(o[x])
// })
// let user = {id: 3, name: 'Albert'}
// let ex3 = function (o) {
//   return safeProp('name',o)
//             .map(fp.first)
// }
// console.log(ex3(user))


let ex4 = (n) => {
   let value = Maybe.of(n)
                .map(parseInt)
    return value
}
console.log(ex4("x11x")._value)